package io.renren.modules.cgs.service.impl;

import io.renren.modules.cgs.dao.CgsPassporapplyDao;
import io.renren.modules.cgs.entity.CgsPassporapplyEntity;
import io.renren.modules.cgs.service.CgsPassporapplyService;
import io.renren.modules.message.dao.MessageDao;
import io.renren.modules.message.entity.MessageEntity;
import io.renren.modules.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("cgsPassporapplyService")
public class CgsPassporapplyServiceImpl implements CgsPassporapplyService {
    @Autowired
    private CgsPassporapplyDao cgsPassporapplyDao;
    @Autowired
    private MessageDao messageDao;

    @Autowired
    private PushService pushService;

    @Override
    public CgsPassporapplyEntity queryObject(String id) {
        return cgsPassporapplyDao.queryObject(id);
    }

    @Override
    public List<CgsPassporapplyEntity> queryList(Map<String, Object> map) {
        return cgsPassporapplyDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return cgsPassporapplyDao.queryTotal(map);
    }

    @Override
    public void save(CgsPassporapplyEntity cgsPassporapply) {
        cgsPassporapplyDao.save(cgsPassporapply);
    }

    @Override
    public void update(CgsPassporapplyEntity cgsPassporapply) {
        // 更新业务表
        cgsPassporapplyDao.update(cgsPassporapply);
        // 新增消息
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setUsername(cgsPassporapplyDao.queryObject(cgsPassporapply.getId()).getApplyUserId());
        messageEntity.setTitle("通行证申请结果");
        String result = "处理中";
        if ("2".equals(cgsPassporapply.getStatus())) {
            result = "拒绝";
        }
        if ("4".equals(cgsPassporapply.getStatus())) {
            result = "通过";
        }
        messageEntity.setMessage("您的通行证书申请结果为：" + result + "\n" +
                "批复路线：" + cgsPassporapply.getVerifyLineName() + "\n" +
                "批复详情信息：" + cgsPassporapply.getStatusExplain());
        messageEntity.setCreatedate(new Date());
        messageEntity.setType("1");
        messageEntity.setReadstate(0);
        messageDao.save(messageEntity);

        // 发送个推消息
        pushService.pushTransmissionMessageToSingle(messageEntity.getUsername(),messageEntity.getType(), String.valueOf(messageEntity.getId()));
    }

    @Override
    public void delete(String id) {
        cgsPassporapplyDao.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        cgsPassporapplyDao.deleteBatch(ids);
    }

}
